<?php

defined('BASEPATH') or die('forbidden');

class Account_Controller Extends Controller {

    private $rolename;
    private $inc;
    private $m;	//数据库连接
    private $sealflag;
    private $areaList;
    private $wholeServerHost;	//全局地址
    private $server_info;
	
    function _init() {
        check_level(50, segment(0), segment(1));
        set_time_limit(0);
        $this->inc = $this->config['inc'];
        
        //得到选择的区号
		$areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }
        $this->sealflag = array("1" => "未封", "0" => "封停");
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
        $server_list=get_server_list();
        foreach($server_list as $k=>$row) {
            $this->areaList[$row['SvrID']] = $row['SvrID']."-".$row['SvrName'][1];
        }

        //query server mapping
        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='6'";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');

        // db数据库
        $this->m = new MongoClient("mongodb://{$this->server_info['dbip']}");

    }

    function index() {
        $this->view->display("dbplayer/account/index");
    }

    function flexigrid() {

        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $query=$_POST['query'];
        $query_type=$_POST['qtype'];
        if($query_type=='_id'){
            $filter=array($query_type=>(int)$query);
        }else{
            $filter=array($query_type=>$query);
        }

        //echo "'<script>alert('.$filter.');</script>'";
        $collection=$this->m->{$this->server_info['dbname']}->Account;

        if($query!=''){
            $document=$collection->find($filter);
        }else{
            $document=$collection->find();
        }
        $data=array();
        $total = count($document);
        //var_dump($document);
        foreach ($document as $key=>$value){
            $value['createtime'] = get_datetime($value['createtime']);
            $value['lasttime'] = get_datetime($value['lasttime']);
            $value['lastsvrid'] = $this->areaList[$value['lastsvrid']]."(".$value['lastsvrid'].")";
            $value['enable'] = $this->sealflag[$value['enable']]."(".$value['enable'].")";
            $data[$key]=array('id' => $value['_id'], 'cell' => $value);
        }

        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $data
        );

        //$this->centerConnt->free_all();
        echo json_encode($result);
    }

}

?>